package com.menu.web.controller.reception;

import com.menu.common.annotation.Log;
import com.menu.common.core.controller.BaseController;
import com.menu.common.core.domain.AjaxResult;
import com.menu.common.enums.BusinessType;
import com.menu.common.utils.poi.ExcelUtil;
import com.menu.system.domain.ReceptionDept;
import com.menu.system.service.IreceptionDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 部门Controller
 *
 * @author ruoyi
 * @date 2024-06-06
 */
@RestController
@RequestMapping("/system/receptionDept")
public class receptionDeptController extends BaseController {
    @Autowired
    private IreceptionDeptService receptionDeptService;

    /**
     * 查询部门列表
     */
    @PreAuthorize("@ss.hasPermi('system:receptionDept:list')")
    @GetMapping("/list")
    public AjaxResult list(ReceptionDept receptionDept) {

        List<ReceptionDept> list = receptionDeptService.selectreceptionDeptList(receptionDept);
        return success(list);
    }

    /**
     * 导出部门列表
     */
    @PreAuthorize("@ss.hasPermi('system:receptionDept:export')")
    @Log(title = "部门", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ReceptionDept receptionDept) {
        List<ReceptionDept> list = receptionDeptService.selectreceptionDeptList(receptionDept);
        ExcelUtil<ReceptionDept> util = new ExcelUtil<ReceptionDept>(ReceptionDept.class);
        util.exportExcel(response, list, "部门数据");
    }

    /**
     * 获取部门详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:receptionDept:query')")
    @GetMapping(value = "/{deptId}")
    public AjaxResult getInfo(@PathVariable("deptId") Long deptId) {
        return success(receptionDeptService.selectreceptionDeptByDeptId(deptId));
    }

    /**
     * 新增部门
     */
    @PreAuthorize("@ss.hasPermi('system:receptionDept:add')")
    @Log(title = "部门", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ReceptionDept receptionDept) {
        return toAjax(receptionDeptService.insertreceptionDept(receptionDept));
    }

    /**
     * 修改部门
     */
    @PreAuthorize("@ss.hasPermi('system:receptionDept:edit')")
    @Log(title = "部门", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ReceptionDept receptionDept) {
        return toAjax(receptionDeptService.updatereceptionDept(receptionDept));
    }

    /**
     * 删除部门
     */
    @PreAuthorize("@ss.hasPermi('system:receptionDept:remove')")
    @Log(title = "部门", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deptIds}")
    public AjaxResult remove(@PathVariable Long[] deptIds) {
        for (Long deptId : deptIds) {
            ReceptionDept receptionDept = new ReceptionDept();
            receptionDept.setParentId(deptId);
            if (!receptionDeptService.selectreceptionDeptList(receptionDept).isEmpty()) {
                return error("存在下级部门不允许删除");
            }


        }


        return toAjax(receptionDeptService.deletereceptionDeptByDeptIds(deptIds));
    }
}
